// import { createRequire } from 'module';
// const require = createRequire(import.meta.url);
const Koa = require('koa')
const cors = require('koa-cors')
const { koaBody } = require('koa-body')
const bodyParser = require('koa-body-parser')
const router = require('koa-Router')()
const Mock = require('mockjs')
const jwt = require('jsonwebtoken')

const data = Mock.mock({
    'list|30': [
        {
            id: '@id',
            name: '@cname',
            'age|23-100': 23,
            'sex|1': ['男', '女'],
            address: '@city'
        }
    ]
})
let userJson = [
    {
        id: '1',
        username: '李帅坤',
        password: '111',
        roleId: '1'
    },
    {
        id: '2',
        username: '傻der',
        password: '111',
        roleId: '2'
    },
    {
        id: '3',
        username: '翠花',
        password: '111',
        roleId: '3'
    },
]
let roleJson = [
    {
        id: '1',
        rolename: '超管',
        authId: '*'
    },
    {
        id: '2',
        rolename: '商家',
        authId: '0,2'
    },
]
let authJson = [
    {
        id: '0',
        name: '概览'
    },
    {
        id: '1',
        name: '系统管理',
        children: [
            {
                id: '1-1',
                name: '菜单管理',
                path: '/home/menu'
            },
            {
                id: '1-2',
                name: '用户管理',
                path: '/home/user'
            },
            {
                id: '1-3',
                name: '角色管理',
                path: '/home/role'
            }
        ]
    },
    {
        id: '2',
        name: '商品管理',
        children: [
            {
                id: '2-1',
                name: '商品列表',
                path: '/home/shop'
            },
            {
                id: '2-2',
                name: '商品分类',
                path: '/home/shoptype'
            }
        ]
    }
]
const app = new Koa()
app.use(cors({
    origin: '*'
}))

router.post('/api/login', ctx => {
    let { username, password } = ctx.request.body;
    let user = userJson.find(item => item.username == username && item.password == password)
    // let token=jwt.sign({
    //     username,
    //     password
    // },'asdfgghherd', {expiresIn: '1h'} )
    if (!user) {
        return (ctx.body = {
            code: 401,
            msg: '用户不存在！',
            // data:{}
        })
    }
    else {
        return (ctx.body = {
            code: 200,
            msg: '登录成功',
            // data:{}
        })
    }

    // let role=roleJson.find(item=>item.id==user.roleId)
    // let auths=[]
    // if(role){
    //     if(role.authId=='*'){
    //         auths=authJson
    //     }else{
    //         auths=authJson.filter(item=>role.authId.includes(item.id))
    //     }
    //     return (ctx.body={
    //         code:200,
    //         msg:'登录成功!',
    //         token:token,
    //         data:{
    //             user:user.username,
    //             role:role.rolename,
    //             auths
    //         }
    //     })
    // }
})

router.get('/api/userlist', ctx => {
    ctx.body = {
        code: 200,
        msg: '登录成功！',
        data: data.list
    }
})
app.use(
    koaBody({
        multipart: true
    })
)

app.use(bodyParser())
app.use(router)
app.listen(9000, () => {
    console.log('服务器启动成功')
})